/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Entity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

/**
 *
 * @author Ardha
 */
public class Pasien {

    private String id;
    private String nama;
    private char jenisKelamin;
    private String alamat;
    private Date tanggalLahir;
    private int umur;
    private float beratBadan;
    private Connection konek = null;
    private static Pasien pasien;
    private static Pasien koneksi;

    public Pasien() {
    }

    public Pasien(Connection konek) {
        this.konek = konek;
    }

    public static Pasien getKoneksiKontrol()
            throws SQLException, FileNotFoundException, IOException {
        if (koneksi == null) {
            koneksi = new Pasien(Koneksi.getDBConnection());
        }
        return koneksi;
    }

    public static void setPasien(Pasien pasien) {
        Pasien.pasien = pasien;
    }

    public static Pasien getPasien() {
        return Pasien.pasien;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the nama
     */
    public String getNama() {
        return nama;
    }

    /**
     * @param nama the nama to set
     */
    public void setNama(String nama) {
        this.nama = nama;
    }

    /**
     * @return the jenisKelamin
     */
    public char getJenisKelamin() {
        return jenisKelamin;
    }

    /**
     * @param jenisKelamin the jenisKelamin to set
     */
    public void setJenisKelamin(char jenisKelamin) {
        this.jenisKelamin = jenisKelamin;
    }

    /**
     * @return the alamat
     */
    public String getAlamat() {
        return alamat;
    }

    /**
     * @param alamat the alamat to set
     */
    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }

    /**
     * @return the tanggalLahir
     */
    public Date getTanggalLahir() {
        return tanggalLahir;
    }

    /**
     * @param tanggalLahir the tanggalLahir to set
     */
    public void setTanggalLahir(Date tanggalLahir) {
        this.tanggalLahir = tanggalLahir;
    }

    /**
     * @return the beratBadan
     */
    public float getBeratBadan() {
        return beratBadan;
    }

    /**
     * @param beratBadan the beratBadan to set
     */
    public void setBeratBadan(float beratBadan) {
        this.beratBadan = beratBadan;
    }

    /**
     * @return the umur
     */
    public int getUmur() {
        return umur;
    }

    /**
     * @param umur the umur to set
     */
    public void setUmur(int umur) {
        this.umur = umur;
    }

    public Pasien cariDataPasien(String id) throws SQLException, FileNotFoundException, IOException {
        String query1 = "select *,((TO_DAYS(CURDATE())- TO_DAYS(tanggal_lahir))/365) AS umur from pasien where Id_Pasien='" + id.toUpperCase() + "'";
        Statement psmt1 = konek.createStatement();
        ResultSet rs1 = psmt1.executeQuery(query1);
        if (rs1.next()) {
            System.out.println("ada");
            String query2 = "select * from rekam_medis where Id_Pasien='" + id.toUpperCase() + "'";
            Statement psmt2 = konek.createStatement();
            ResultSet rs2 = psmt2.executeQuery(query2);
            if (rs2.next()) {
                System.out.println("rekam medis ada");
                String query = "select p.id_pasien, p.nama_pasien, p.jenis_kelamin, p.alamat, p.tanggal_lahir,"
                        + "((TO_DAYS(CURDATE())- TO_DAYS(p.tanggal_lahir))/365) AS umur, r.berat_badan "
                        + " from pasien p, rekam_medis r where p.id_pasien=r.id_pasien and upper(p.id_pasien)= '" + id.toUpperCase() + "'"
                        + " AND r.no_rekam_medis=(SELECT MAX(No_Rekam_Medis) "
                        + "FROM rekam_medis WHERE upper(id_pasien)= '" + id.toUpperCase() + "')";

                Statement psmt = konek.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
                ResultSet rs = psmt.executeQuery(query);

                Pasien p = new Pasien();
                if (rs.first()) {
                    p.setId(rs.getString(1));
                    p.setNama(rs.getString(2));
                    p.setJenisKelamin(rs.getString(3).charAt(0));
                    p.setAlamat(rs.getString(4));
                    p.setTanggalLahir(rs.getDate(5));
                    String umr = rs.getObject(6).toString();
                    int titik = umr.indexOf('.');
                    p.setUmur(Integer.parseInt(umr.substring(0, titik)));
                    p.setBeratBadan(rs.getFloat(7));
                }
                Pasien.setPasien(p);

                return Pasien.getPasien();
            } else {
                System.out.println("rekam medis tidak ada");
                Pasien p = new Pasien();
                if (rs1.first()) {
                    p.setId(rs1.getString(1));
                    p.setNama(rs1.getString(2));
                    p.setJenisKelamin(rs1.getString(3).charAt(0));
                    p.setAlamat(rs1.getString(5));
                    p.setTanggalLahir(rs1.getDate(4));
                    String umr = rs1.getObject("umur").toString();
                    int titik = umr.indexOf('.');
                    p.setUmur(Integer.parseInt(umr.substring(0, titik)));
                    p.setBeratBadan(rs1.getFloat(6));
                }
                Pasien.setPasien(p);
                return Pasien.getPasien();
            }
//            return true;
        } else {
            System.out.println("pasien tidak ada");
            Pasien p = new Pasien();
                    p.setId("");
                    p.setNama("");
                    Pasien.setPasien(p);
                
            return Pasien.getPasien();
//            return false;
        }
    }

    public int isiTabelRekamMedis(Pasien pasien) throws SQLException, FileNotFoundException, IOException {
        String sql = "{CALL cek_pasien(?)}";
        CallableStatement callst = konek.prepareCall(sql);
        callst.setObject(1, pasien.getId());
        ResultSet rs = callst.executeQuery();
        int notif = 0;
        if (rs.first()) {
            notif = rs.getInt(1);
        }
        callst.close();
        return notif;
    }

}
